package com.unitd.app.param.controller;

import com.unitd.frame.sso.common.config.SSOConfig;
import com.unitd.frame.sso.common.config.SSOProperties;
import com.unitd.frame.sso.common.helper.SSOHelper;
import com.unitd.frame.sso.common.token.SSOToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.URLEncoder;

/**
 * @desc 程序首页控制器
 * @filename IndexController.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/4
 */
@Controller
public class IndexController extends BaseController {

	@RequestMapping("/index")
	public String index(Model model) {
		SSOToken token = SSOHelper.getToken(request);
		if (token == null) {
			SSOProperties prop = SSOConfig.getSSOProperties();
			String loginUrl = prop.get("sso.login.url"); 					// SSO服务端登陆URL
			String proxyUrl = prop.get("sso.defined.proxyurl"); 			// SSO客户端登陆服务端的代理访问地址
			String paramReturl = SSOConfig.getInstance().getParamReturl(); 	// 通过SSO登陆后,回调地址前的请求参数名

			// 重定向至代理跨域地址页
			String returnUrl = loginUrl + "?" + paramReturl + "=" + URLEncoder.encode(proxyUrl);
			return redirectTo(returnUrl);
		} else {
			model.addAttribute("userId", token.getUid());
		}
		return "index";
	}
}